package com.zhou.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.org.dao.UserPasswordRecord;
import com.zhou.org.dto.ChangePasswordDto;
import com.zhou.org.mapper.UserPasswordRecordMapper;
import com.zhou.org.service.UserPasswordRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lang.zhou
 * @since 2023-02-26
 */
@Service
public class UserPasswordRecordServiceImpl extends ServiceImpl<UserPasswordRecordMapper, UserPasswordRecord> implements UserPasswordRecordService {

    @Override
    public List<UserPasswordRecord> listRecord(Integer userId) {
        LambdaQueryWrapper<UserPasswordRecord> w = new LambdaQueryWrapper<>();
        w.eq(UserPasswordRecord::getUserId,userId);
        w.orderByDesc(UserPasswordRecord::getCreateDt);
        return list(w);
    }

    @Override
    public List<UserPasswordRecord> listRecordCount(Integer userId, int time) {
        LambdaQueryWrapper<UserPasswordRecord> w = new LambdaQueryWrapper<>();
        w.eq(UserPasswordRecord::getUserId,userId);
        w.orderByDesc(UserPasswordRecord::getCreateDt);
        IPage<UserPasswordRecord> page = new Page<>();
        page.setSize(time);
        page.setPages(1);
        IPage<UserPasswordRecord> resPage = page(page);
        return resPage.getRecords();
    }

    @Override
    public void saveRecord(Integer userId, String oldPwd, String newPwd) {
        UserPasswordRecord record = new UserPasswordRecord();
        record.setUserId(userId);
        record.setNewPassword(newPwd);
        record.setOldPassword(oldPwd);
        record.setCreateDt(new Date());
        save(record);
    }
}
